package psn.hui.bus.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import psn.hui.bus.domain.Customer;
import psn.hui.bus.mapper.CustomerMapper;
import psn.hui.bus.service.CustomerService;
import psn.hui.bus.vo.CustomerVo;
import psn.hui.sys.utils.DataGridView;

/**
 * 	客户管理服务接口实现类
 * 
 * @author hui
 *
 */
@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerMapper customerMapper;

	/**
	 * 	插入客户数据，动态sql(可选字段)
	 * 
	 * @param customer
	 * @return
	 */
	@Override
	public int addCustomer(Customer customer) {
		customer.setCreatetime(new Date());
		return customerMapper.insertSelective(customer);
	}

	/**
	 * 	根据主键(identity)删除客户
	 * 
	 * @param identity
	 * @return
	 */
	@Override
	public int deleteByIdentity(String identity) {
		return customerMapper.deleteByPrimaryKey(identity);
	}

	/**
	 * 根据主键(identity)更新客户数据，动态sql(可选字段)
	 * 
	 * @param customer
	 * @return
	 */
	@Override
	public int updateByIdentity(Customer customer) {
		return customerMapper.updateByPrimaryKeySelective(customer);
	}

	/**
	 * 根据主键(identity)查询客户
	 * 
	 * @param identity
	 * @return
	 */
	@Override
	public Customer selectByIdentity(String identity) {
		return customerMapper.selectByPrimaryKey(identity);
	}

	/**
	 * 根据可选字段查询客户
	 * 
	 * @param customerVo
	 * @return
	 */
	@Override
	public DataGridView queryAllCustomer(CustomerVo customerVo) {
		Page<Object> page = PageHelper.startPage(customerVo.getPage(), customerVo.getLimit());
		List<Customer> customers = customerMapper.queryAllCustomer(customerVo);
		return new DataGridView(page.getTotal(), customers);
	}
	/**
	 * 	根据identity批量删除客户数据
	 * @param identitys
	 * @return
	 */
	public int batchDeleteCustomerByIds(String[] identitys) {
		int rs = 0;
		for (String identity : identitys) {
			rs += customerMapper.deleteByPrimaryKey(identity);
		}
		return rs;
	}
	/**
	 * 	检查身份证号是否可用(不重复)
	 * @param identity
	 * @return 该身份证存在返回false,否则true
	 */
	public boolean checkIdentity(String identity) {
		return null == customerMapper.selectByPrimaryKey(identity);
	}
	/**
	 * 	查询客户列表
	 */
	@Override
	public List<Customer> queryAllCustomerForList(CustomerVo customerVo) {
		return customerMapper.queryAllCustomer(customerVo);
	}
}